package com.daybook.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.daybook.pojo.Category;
import com.daybook.service.CategoryService;
import com.daybook.util.PageHelp;

@Controller
@RequestMapping(value = "/categoryMsg")
public class CategoryController {
	@Autowired
	private CategoryService categoryService;

	// 条件查询获取类别列表
	@RequestMapping(value = "/getStuList", method = RequestMethod.POST)
	public void getCategoryList(HttpServletRequest request, HttpServletResponse response)
			throws IOException, JSONException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		// 获得页码
		String num = request.getParameter("num");
		// 获得查询的类别名
		String cname = request.getParameter("cname");
		int pageNow = Integer.valueOf(num);
		PageHelp page = categoryService.getLimitPageList(pageNow, cname);
		// 获取总页数
		int totlpage = page.getTotalPageCount();
		// 强制类型转换
		List<Category> stuList = (List<Category>) page.getList();
		PrintWriter out = response.getWriter();
		JSONObject obj = new JSONObject();
		obj.put("res", 1);
		obj.put("totalPage", totlpage);
		obj.put("num", stuList.size());
		obj.put("list", stuList);
		out.print(obj);
	}
	// 询获取所有类别列表
		@RequestMapping(value = "/getAllCategory", method = RequestMethod.POST)
		public void getAllCategoryList(HttpServletRequest request, HttpServletResponse response)
				throws IOException, JSONException {
			request.setCharacterEncoding("utf-8");
			response.setContentType("text/html;charset=utf-8");
			
			// 强制类型转换
			List<Category> stuList = categoryService.selectAllCategory();
			PrintWriter out = response.getWriter();
			JSONObject obj = new JSONObject();
			obj.put("list", stuList);
			out.print(obj);
		}
	// 类别更新
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public void updateCategory(HttpServletRequest request, HttpServletResponse response, Category category)
			throws IOException, JSONException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		int result = categoryService.updateCategory(category);
		PrintWriter out = response.getWriter();
		JSONObject obj = new JSONObject();
		obj.put("res", result);
		out.print(obj);
	}

	// 类别添加
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public void addCategory(HttpServletRequest request, HttpServletResponse response, Category category)
			throws IOException, JSONException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		int result = -1;
		result = categoryService.addCategory(category);
		PrintWriter out = response.getWriter();
		JSONObject obj = new JSONObject();
		// 类别不存在，可以注册
		obj.put("res", result);
		out.print(obj);
		out.flush();
		out.close();
	}

	// 类别添加
	@RequestMapping(value = "/yanzheng", method = RequestMethod.POST)
	public void getOldCategory(HttpServletRequest request, HttpServletResponse response)
			throws IOException, JSONException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		String cname = request.getParameter("cname");
		int result = -1;
		result = categoryService.getOldCategory(cname);
		PrintWriter out = response.getWriter();
		JSONObject obj = new JSONObject();
		// 类别不存在，可以注册
		if (result == 0) {
			obj.put("valid", true);
		} else {
			obj.put("valid", false);
		}
		out.print(obj);
		out.flush();
		out.close();
	}

	// 类别删除
	@RequestMapping(value = "/del", method = RequestMethod.POST)
	public void delCategory(HttpServletRequest request, HttpServletResponse response)
			throws IOException, JSONException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		String id = request.getParameter("cid");
		int cid = Integer.valueOf(id);
		int result = -1;
		result = categoryService.delCategory(cid);
		PrintWriter out = response.getWriter();
		JSONObject obj = new JSONObject();
		obj.put("res", result);
		out.print(obj);
		out.flush();
		out.close();
	}

}
